using UnityEngine.UI;
using UnityEngine;

/// <summary>
/// 不读取换行空格的Text组件
/// </summary>
[RequireComponent(typeof(Text))]
public class NonBreakingSpaceText : MonoBehaviour
{
    private Text txt;//Text文本组件

    private const string NonBreakingSpace = "\u00A0";//不换行空格的Unicode编码

    private void Awake()
    {
        txt = GetComponent<Text>();

        OnTextChange();
        txt.RegisterDirtyLayoutCallback(OnTextChange);
    }

    private void OnTextChange()
    {
        if (txt.text.Contains(" "))
        {
            txt.text = txt.text.Replace(" ", NonBreakingSpace);
        }
    }
}